package com.blog.login.service.impl;


import com.blog.login.entity.Role;
import com.blog.login.entity.User;
import com.blog.login.entity.UserRole;
import com.blog.login.mapper.UserMapper;
import com.blog.login.mapper.UserRoleMapper;
import com.blog.login.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.utils.GetSalt;
import com.blog.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苏离
 * @since 2021-12-08
 */
@Service
@Slf4j
@Transactional
@SuppressWarnings("all")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
UserMapper userMapper;
@Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public String findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    /**
     * 进行密码加密
     * @param password 密码
     * @param salt 随机盐
     * @return 加密后的密码
     */
    private String getPassword(String password, String salt){
        Md5Hash md5Hash=new Md5Hash(password,salt,1024);
        return md5Hash.toHex();
    }
    /**用户注册
     *
     * @param user 实体类
     * @return 是否注册成功
     */
    @Override
    public boolean registerUser(User user) {
        try {
            String salt = GetSalt.getSalt();
            user.setUserSalt(salt);
            String password = getPassword(user.getUserPassword(), salt);
            user.setUserPassword(password);
            user.setUserPet(user.getUserName());
            userMapper.insert(user);
            //获取插入后的id值
            String userId = user.getUserId();
            UserRole userRole= new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(1);
            userRoleMapper.insert(userRole);
            return true;
        } catch (Exception e) {
            return false;
        }

    }


    /**
     * 处理登录
     * @param userName 用户名
     * @param password 密码
     * @return Map数据
     */
    @Override
    public Map<String, Object> login(String userName, String password) {
        User user = userMapper.getUser(userName);
        Map<String,Object> map=new HashMap<>();
        //用户名和密码验证
        if(ObjectUtils.isEmpty(user)){
            log.info("用户："+userName+"用户名错误");
            map.put("message", "用户名错误");
            return map;
        }
        String dbpassword = getPassword(password, user.getUserSalt());
        if (!dbpassword.equals(user.getUserPassword())) {
            log.info("用户："+userName+"密码错误");
            map.put("message", "密码错误");
            return map;
        }
        if(user.getUserFreeze()==1){
            log.info("用户："+userName+"被冻结");
            map.put("message", "账号已冻结");
            return map;
        }
        //当验证通过将token都返回。
        List<String> userRole = userMapper.getUserRole(user.getUserId());
        String token = JWTUtils.getJwtToken(userName,
                                            userRole,
                                            user.getUserPet(),
                                            user.getUserId(),
                                            user.getUserPhoto(),
                                            user.getUserDescription());
        map.put("token", token);
        return map;
    }

    /**密码更新
     *
     * @param username 用户名
     * @param password 密码
     * @return 密码是否更新成功
     */
    @Override
    public Boolean updatePassword(String username, String password) {
        String salt = GetSalt.getSalt();
        String passwords = getPassword(password, salt);
        Integer integer = userMapper.updateUserPassword(username, passwords, salt);
        if(integer==1)
        return true;
        else return false;
    }
}
